package com.niit.house.lyl.ownership;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.util.HashMap;

import java.io.IOException;


public class OwnershipReducer extends Reducer<Text, Text, Text, Text> {
    private int totalHouses = 0;

    HashMap<Integer, String> Types = new HashMap<Integer, String>();


    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        int houses = 0;

        for (Text val : values) {
            houses ++;
        }

        Types.put(houses, key.toString());
        totalHouses += houses;
    }



    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {

        for (Integer i: Types.keySet()) {
            double ownershipRate = (double) i / totalHouses;
            context.write(new Text(""), new Text(String.format("交易权属: %s, 占比: %.2f%%", Types.get(i), ownershipRate * 100)));
        }

    }
}